package com.zhentao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.mapper.DataExceptionMapper;
import com.zhentao.pojo.DataException;
import com.zhentao.service.DataExceptionService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author 李佳隆
* @description 针对表【data_exception(数据异常表：记录采集/入库环节的异常数据及处理情况)】的数据库操作Service实现
* @createDate 2025-08-27 09:50:25
*/
@Service
public class DataExceptionServiceImpl extends ServiceImpl<DataExceptionMapper, DataException>
    implements DataExceptionService{

    @Override
    public List<DataException> getPageList(Integer page, Integer size, Map<String, Object> queryParams) {
        // 创建分页对象
        Page<DataException> pageObj = new Page<>(page, size);
        
        // 构建查询条件
        QueryWrapper<DataException> queryWrapper = new QueryWrapper<>();
        
        if (queryParams != null) {
            // 异常类型条件
            if (queryParams.get("exceptionType") != null) {
                queryWrapper.eq("exception_type", queryParams.get("exceptionType"));
            }
            
            // 处理状态条件
            if (queryParams.get("handleStatus") != null) {
                queryWrapper.eq("handle_status", queryParams.get("handleStatus"));
            }
            
            // 采集数据ID条件
            if (queryParams.get("collectionId") != null) {
                queryWrapper.eq("collection_id", queryParams.get("collectionId"));
            }
        }
        
        // 按ID倒序排列
        queryWrapper.orderByDesc("id");
        
        // 执行分页查询
        IPage<DataException> result = this.page(pageObj, queryWrapper);
        
        return result.getRecords();
    }

    @Override
    public Long countByType(Integer exceptionType) {
        QueryWrapper<DataException> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("exception_type", exceptionType);
        return this.count(queryWrapper);
    }

    @Override
    public Long countByStatus(Integer handleStatus) {
        QueryWrapper<DataException> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("handle_status", handleStatus);
        return this.count(queryWrapper);
    }

}




